#!/usr/bin/env python

import sys

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

import pygame
from pygame.locals import *

from game import *

if len(sys.argv) < 2:
    print "Please specify a level file to load as follows: data.levels.level*, making sure to leave out the .py extension"
    sys.exit()



def setup(width, height):
    pygame.init()
    glutInit([])

    flags = OPENGL | DOUBLEBUF | HWSURFACE

    screen = pygame.display.set_mode((width, height), flags)
    pygame.display.toggle_fullscreen()
    pygame.mouse.set_visible(False)

    glMatrixMode(GL_PROJECTION)
    gluPerspective(40, 1.0*width/height, 0.1, 1000.0)
    glMatrixMode(GL_MODELVIEW)
		
    glEnable(GL_DEPTH_TEST)
    
    return screen


setup(1280, 768)

#screen = pygame.event.set_blocked(MOUSEMOTION)

game = Game()

exec 'import ' + sys.argv[1] + ' as level'
level.init(game)

done = False
fps = 30.0
wait_ticks = 20
last_count = pygame.time.get_ticks()
while not done:

    glClearColor(0.15, 0.17, 0.2, 1.0)    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
   
    game.step(fps)
    game.draw()
    
    pygame.display.flip()
    
    if game.do_input():
        done = True

    while pygame.time.get_ticks() < last_count + wait_ticks:
        pass
    
    last_count = pygame.time.get_ticks()
